package com.wenyu.wenyuyuedu.adapter.homepagefragment;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.bean.homepager.DeityAttentionBean;
import com.wenyu.wenyuyuedu.utils.TimeStyle;
import com.wenyu.wenyuyuedu.view.CircleImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/7.
 * 首页---神贴
 */

public class DeitryAttentionAdapter extends BaseAdapter {
    private List<DeityAttentionBean.InfoBean> info = new ArrayList<>();
    private Context context;
    private View view;

    private DeitryAttentionHolder holder;

    public DeitryAttentionAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return info.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        DeitryAttentionHolder holder = null;
        DeityAttentionBean.InfoBean infoBean = info.get(i);
        if (view == null) {
            view = View.inflate(context, R.layout.homepage_deityattentin_item, null);
            holder = new DeitryAttentionHolder();
            holder.head_view = (CircleImageView) view.findViewById(R.id.home_all_head);
            holder.deityPaster = (ImageView) view.findViewById(R.id.home_all_shen);
            holder.nickName = (TextView) view.findViewById(R.id.hoeme_all_nickname);
            holder.releaseTime = (TextView) view.findViewById(R.id.home_all_releasetime);
            holder.classIfy = (TextView) view.findViewById(R.id.home_all_classify);
            holder.content = (TextView) view.findViewById(R.id.home_all_content);
            holder.contentOne = (ImageView) view.findViewById(R.id.home_all_content_picture_one);
            holder.contentTwo = (ImageView) view.findViewById(R.id.home_all_content_picture_two);
            holder.contentThree = (ImageView) view.findViewById(R.id.home_all_content_picture_three);
            holder.contentFour = (ImageView) view.findViewById(R.id.home_all_content_picture_four);
            holder.contentFive = (ImageView) view.findViewById(R.id.home_all_content_picture_five);
            holder.pictureCount = (TextView) view.findViewById(R.id.home_all_picture_count);
            holder.message = (ImageView) view.findViewById(R.id.home_all_message);
            holder.messageCount = (TextView) view.findViewById(R.id.home_all_messagecount);
            holder.zan = (ImageView) view.findViewById(R.id.home_all_zan);
            holder.zanCount = (TextView) view.findViewById(R.id.home_all_zancount);
            holder.playTour = (ImageView) view.findViewById(R.id.home_all_playtour);
            holder.playTourCount = (TextView) view.findViewById(R.id.home_all_playtourcount);
            holder.iv_allattention_transparency = (ImageView) view.findViewById(R.id.iv_allattention_transparency);
            view.setTag(holder);
        } else {
            holder = (DeitryAttentionHolder) view.getTag();
        }
        this.holder = holder;
        if (info.size() != 0) {
            /*
            * 头像
            * */
            Glide.with(context)
                    .load(infoBean.getPath())
                    .error(R.drawable.noamal_head_photo)
                    .into(holder.head_view);
            /*
            * 昵称
            * */
            holder.nickName.setText(infoBean.getNickname());
            /*
            * 点赞数量
            * */
            String support_num = infoBean.getSupport_num();
            int zanCount = Integer.parseInt(support_num);
            /*
            * 打赏数量
            * */
            String reward_num = infoBean.getReward_num();
            if (zanCount >= 5) {
                holder.deityPaster.setVisibility(View.VISIBLE);
                holder.playTour.setVisibility(View.VISIBLE);
                holder.playTourCount.setVisibility(View.VISIBLE);
                holder.playTourCount.setText(reward_num);
            } else {
                holder.deityPaster.setVisibility(View.GONE);
                holder.playTour.setVisibility(View.GONE);
                holder.playTourCount.setVisibility(View.GONE);
            }
            holder.zanCount.setText(support_num);
            /*
            * 圈子的名称
            * */
            holder.classIfy.setText(infoBean.getTitle());
            /*
            * 帖子的内容
            * */
            String content = infoBean.getContent();
            if(content.equals("")){
                holder.content.setText(content);
                holder.content.setVisibility(View.GONE);

            }else {
                holder.content.setVisibility(View.VISIBLE);
                holder.content.setText(content);
            }

            /*帖子的回复量*/
            holder.messageCount.setText(infoBean.getReply_num());
            /*
            * 发布时间
            * */
            String create_time = infoBean.getCreate_time();
            String s = TimeStyle.publishTime(create_time);
            holder.releaseTime.setText(s);
            /*
            * 内容的图片
            * */
            ArrayList<ImageView> imageViews = new ArrayList<>();
            imageViews.add(holder.contentOne);
            imageViews.add(holder.contentTwo);
            imageViews.add(holder.contentThree);
            imageViews.add(holder.contentFour);
            imageViews.add(holder.contentFive);
            List<String> images = infoBean.getImages();
            holder.pictureCount.setVisibility(View.GONE);
            holder.iv_allattention_transparency.setVisibility(View.GONE);
            // 全部隐藏
            for (ImageView imageView : imageViews) {
                imageView.setVisibility(View.GONE);
            }
            if (images != null && images.size() >= 1) {
                // 根据个数显示
                for (int i1 = 0; i1 < images.size(); i1++) {
                    if (i1 < 5) {
                        imageViews.get(i1).setVisibility(View.VISIBLE);
                        Glide.with(context)
                                .load(infoBean.getImages().get(i1))
                                .error(R.drawable.nomal_dynamic)
                                .into(imageViews.get(i1));
                    } else {
                        holder.iv_allattention_transparency.setVisibility(View.VISIBLE);
                        holder.pictureCount.setVisibility(View.VISIBLE);
                        holder.pictureCount.setText("+" + String.valueOf(images.size() - 5));
                        break;
                    }
                }
            }
        }
        return view;
    }

    class DeitryAttentionHolder {
        CircleImageView head_view;
        ImageView deityPaster;
        TextView nickName;
        TextView releaseTime;
        TextView classIfy;
        TextView content;
        ImageView contentOne;
        ImageView contentTwo;
        ImageView contentThree;
        ImageView contentFour;
        ImageView contentFive;
        TextView pictureCount;
        ImageView message;
        TextView messageCount;
        ImageView zan;
        TextView zanCount;
        ImageView playTour;
        TextView playTourCount;
        ImageView iv_allattention_transparency;
    }

    public void addAll(List<DeityAttentionBean.InfoBean> info) {
        if (info != null) {
            this.info = info;
            notifyDataSetChanged();
        }

    }
}
